<template>
	<view class="detail-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="代表说"></uni-navbar></view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="form-item" required label="正文内容" prop="statement">
					<u-input v-model="form.statement" type="textarea" placeholder="请输入您的看法" :clearable="false" :height="160" :maxlength="-1" />
				</u-form-item>

				<u-form-item class="form-item" label="上传文件">
					<uni-file-upload style="width: 500rpx" type="image" :limit="9" :files="imgList"></uni-file-upload>
				</u-form-item>

				<u-form-item class="form-item" required label="谁可以看" prop="authority">
					<u-input v-model="authority" type="select" :select-open="authorityShow" placeholder="请选择" @click="authorityShow = true" />
					<u-select :list="authorityList" v-model="authorityShow" @confirm="authorityConfirm"></u-select>
				</u-form-item>
			</u-form>
		</view>

		<view class="fiexd-btm"><u-button class="form-btn" type="error" @click="confirm">提交</u-button></view>
	</view>
</template>

<script>
import { mapState } from 'vuex';
import uniFileUpload from '@/components/uni-file-uploader.vue';
export default {
	components: {
		uniFileUpload
	},
	data() {
		return {
			authority: '公开',
			authorityShow: false,
			authorityList: [
				{
					value: '1',
					label: '公开'
				},
				{
					value: '2',
					label: '好友可见'
				},
				{
					value: '3',
					label: '私密'
				}
			],

			imgList: [],

			form: {
				authority: '1',
				clientCode: '',
				createBy: '',
				statement: ''
			},
			rules: {
				statement: [
					{
						required: true,
						message: '请输入看法',
						trigger: 'change'
					}
				],
				authority: [
					{
						required: true,
						message: '请选择可见范围',
						trigger: 'change'
					}
				]
			}
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.img) {
			this.imgList.push(JSON.parse(option.img));
		}
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	onUnload() {
		this.$store.commit('filePush', []);
	},
	computed: {
		...mapState(['userInfo', 'fileList'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
			} else {
				this.$logout();
			}
		},
		authorityConfirm(e, idx) {
			this.authority = e[0].label;
			this.form.authority = e[0].value;
		},
		confirm() {
			this.$refs.uForm.validate((valid) => {
				if (!valid) {
					return false;
				}
				let data = {
					content: this.form.statement
				};
				this.$request('/msg_sec_check/content', data, {
					load: true
				}).then((res) => {
					if (res.data) {
						this.sendContent();
					} else {
						this.$msg('包含敏感词汇！');
					}
				});
			});
		},
		sendContent() {
			let data = {};
			this.form.clientCode = this.userInfo.sysUser.clientCode;
			this.form.createBy = this.userInfo.sysUser.id;

			data = {
				memberStatement: this.form,
				file: this.fileList
			};
			this.$request('/tanghe-weiyuanshuo/member-statement/add', data, {
				load: true
			}).then((res) => {
				this.$msg(res.msg);
				setTimeout(() => {
					let pre = this.$prePage();
					if (pre && pre.loadData) {
						pre.tabsCurrent = 1;
						pre.loadData('refresh');
					}
					uni.navigateBack();
				}, 500);
			});
		}
	}
};
</script>

<style lang="scss">
.detail-view {
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid $u-border-color;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
